﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using BUS;
using DTO;

namespace Presentation
{
    public partial class BM1GUI : Form
    {
        public BM1GUI()
        {
            InitializeComponent();
        }

        //ham load lai datagridview
        public void ShowMaster()
        {
            this.dgvXe.AutoGenerateColumns = false;
            int maloaihang = int.Parse(cbxLoaiHang.SelectedValue.ToString());
            int mahangxe = int.Parse(cbxHangXe.SelectedValue.ToString());
            int matrongtai = int.Parse(cbxTrongTai.SelectedValue.ToString());
            dgvXe.DataSource = XeBUS.SelectXeByAll(maloaihang, mahangxe, matrongtai);
        }

        public void InitHangXe()
        {
            IList list;
            HangXeBUS hangxe = new HangXeBUS();
            list = hangxe.DanhSach();
            cbxHangXe.DataSource = list;
            cbxHangXe.DisplayMember = "TenHangXe";
            cbxHangXe.ValueMember = "MaHangXe";
        }

        public void InitLoaiHang()
        {
            IList list;
            LoaiHangBUS loaihang = new LoaiHangBUS();
            list = loaihang.DanhSach();
            cbxLoaiHang.DataSource = list;
            cbxLoaiHang.DisplayMember = "TenLoaiHang";
            cbxLoaiHang.ValueMember = "MaLoaiHang";
        }

        public void InitTrongTai()
        {
            IList list;
            TrongTaiBUS trongtai = new TrongTaiBUS();
            list = trongtai.DanhSach();
            cbxTrongTai.DataSource = list;
            cbxTrongTai.DisplayMember = "TrongTai";
            cbxTrongTai.ValueMember = "MaTrongTai";
        }

        public void InitNhanVien()
        {
            IList list;
            NhanVienBUS nhanvien = new NhanVienBUS();
            list = nhanvien.DanhSach();
            cbxNhanVien.DataSource = list;
            cbxNhanVien.DisplayMember = "TenNhanVien";
            cbxTrongTai.ValueMember = "MaNhanVien";
        }

        
        private void BM1GUI_Load(object sender, EventArgs e)
        {
            InitNhanVien();
            InitTrongTai();
            InitLoaiHang();
            InitHangXe();
            //ShowMaster();
        }

        private void dgvXe_SelectionChanged(object sender, EventArgs e)
        {
            if (this.dgvXe.CurrentRow != null)
            {
               this.tbxBienSo.Text = this.dgvXe.CurrentRow.Cells["BienSo"].Value.ToString();
               this.tbxSoKhung.Text = this.dgvXe.CurrentRow.Cells["SoKhung"].Value.ToString();
               this.tbxSoMay.Text = this.dgvXe.CurrentRow.Cells["SoMay"].Value.ToString();
               this.tbxNgayDangKiem.Text = this.dgvXe.CurrentRow.Cells["NgayDangKiem"].Value.ToString();
               this.tbxNamSanXuat.Text = this.dgvXe.CurrentRow.Cells["NamSanXuat"].Value.ToString();
               this.tbxHieuXe.Text = this.dgvXe.CurrentRow.Cells["HieuXe"].Value.ToString();
               this.tbxDungTichBinh.Text = this.dgvXe.CurrentRow.Cells["DungTichBinh"].Value.ToString();
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.tbxBienSo.Text = "";
            this.tbxNgayTiepNhan.Text = "";
            this.tbxNgayDangKiem.Text = "";
            this.tbxDungTichBinh.Text = "";
            this.tbxHieuXe.Text = "";
            this.tbxNamSanXuat.Text = "";
            this.tbxSoMay.Text = "";
            this.tbxSoKhung.Text = "";
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            XeDTO xe = new XeDTO();

            xe.NgayTiepNhan = DateTime.Parse(this.tbxNgayTiepNhan.Text.ToString());
            xe.MaNhanVien = int.Parse(this.cbxNhanVien.SelectedValue.ToString());
            xe.BienSo = this.tbxBienSo.Text;
            xe.MaHangXe = int.Parse(this.cbxHangXe.SelectedValue.ToString());
            xe.MaLoaiHang = int.Parse(this.cbxLoaiHang.SelectedValue.ToString());
            xe.MaTrongTai = int.Parse(this.cbxTrongTai.SelectedValue.ToString());
            xe.SoKhung = this.tbxSoKhung.Text;
            xe.SoMay = int.Parse(this.tbxSoKhung.Text.ToString());
            xe.NgayDangKiem = DateTime.Parse(this.tbxNgayDangKiem.Text.ToString());
            xe.NamSanXuat = int.Parse(tbxNamSanXuat.Text);
            xe.DungTichBinh = int.Parse(tbxDungTichBinh.Text);
            
            bool result = XeBUS.InsertXe(xe);
            if(result == true)
            {
                MessageBox.Show("Successful!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ShowMaster();
            }
            else
            {
                MessageBox.Show("Fail!\r\nChooser new MaXe,Please!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            XeDTO xe = new XeDTO();

            //xe.MaXe = this.tbxMaXe.Text;
            xe.NgayTiepNhan = DateTime.Parse(this.tbxNgayTiepNhan.Text.ToString());
            xe.MaNhanVien = int.Parse(this.cbxNhanVien.SelectedValue.ToString());
            xe.BienSo = this.tbxBienSo.Text;
            xe.MaLoaiHang = int.Parse(this.cbxLoaiHang.SelectedValue.ToString());
            xe.MaHangXe = int.Parse(this.cbxHangXe.SelectedValue.ToString());
            xe.MaTrongTai = int.Parse(this.cbxTrongTai.SelectedValue.ToString());
            xe.NamSanXuat = int.Parse(this.tbxNamSanXuat.Text);
            xe.NgayDangKiem = DateTime.Parse(this.tbxNgayDangKiem.Text.ToString());
            xe.NamSanXuat = int.Parse(this.tbxNamSanXuat.Text);
            xe.SoKhung = this.tbxSoKhung.Text;
            xe.SoMay = int.Parse(this.tbxSoMay.Text);
            xe.HieuXe = this.tbxHieuXe.Text;
            xe.DungTichBinh = int.Parse(this.tbxDungTichBinh.Text);

            bool result = XeBUS.UpdateXe(xe);
            if(result == true)
            {
                MessageBox.Show("Successful!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ShowMaster();
            }
            else
            {
                MessageBox.Show("Fail!\r\nCheck MaXe,Please!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgvXe.CurrentRow != null)
            {
                int MaXe = int.Parse(this.dgvXe.CurrentRow.Cells["MaXe"].Value.ToString());
                DialogResult dr = MessageBox.Show("Do you want to delete MaXe " + MaXe + "?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dr == DialogResult.Yes)
                {
                    bool result = XeBUS.DeleteXeByMaXe(MaXe);
                    if (result == true)
                    {
                        MessageBox.Show("Successful!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.ShowMaster();
                    }
                    else
                    {
                        MessageBox.Show("Fail!\r\nCheck MaXe,Please!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
                MessageBox.Show("Fail!\r\nChoose one Xe,Please!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Do you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dr == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void cbxLoaiHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitLoaiHang();
            ShowMaster();
        }

        private void cbxHangXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitHangXe();
            ShowMaster();
        }

        private void cbxTrongTai_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitTrongTai();
            ShowMaster();
        }

        private void cbxNhanVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitNhanVien();
        }
    }
}